//
//  TechnicalViewController.m
//  AdvertisementI
//
//  Created by mac on 16/5/19.
//  Copyright © 2016年 Apple inc. All rights reserved.
//

#import "TechnicalViewController.h"
#import "FCXRefreshFooterView.h"
#import "FCXRefreshHeaderView.h"
#import "UIScrollView+FCXRefresh.h"

#import "netWork.h"
#import "MAin.h"
@interface TechnicalViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger rows;
    UITableView *mTableView;
    FCXRefreshHeaderView *headerView;
    FCXRefreshFooterView *footerView;
}
@end
@implementation TechnicalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    //设置导航栏字体颜色为白色
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:19],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    //..................
    if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)])
    {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    //NSLog(@"width:%f height:%f",kScreenW,kScreenH);
    rows = 10;
    mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kScreenW, kScreenH-104) style:UITableViewStylePlain];
    mTableView.tag = 1;
    mTableView.delegate = self;
    mTableView.dataSource = self;
    mTableView.rowHeight = 80;   //设置行高
    [self.view addSubview:mTableView];
    
    [self addRefreshView];
    [self JsonAnalyze];
}
- (void)JsonAnalyze{
    _titleArray=[[NSMutableArray alloc]init];
    [netWork scroll:^(NSArray *arr) {
        [_titleArray setArray:arr];
        [mTableView reloadData];
    } type:@"app_loadType?id=0"];
}

- (void)addRefreshView {
    
    __weak __typeof(self)weakSelf = self;
    
    //下拉刷新
    headerView = [mTableView addHeaderWithRefreshHandler:^(FCXRefreshBaseView *refreshView) {
        [weakSelf refreshAction];
    }];
    
    //上拉加载更多
    footerView = [mTableView addFooterWithRefreshHandler:^(FCXRefreshBaseView *refreshView) {
        [weakSelf loadMoreAction];
    }];
    
    //自动刷新
    footerView.autoLoadMore = self.autoLoadMore;
}

- (void)refreshAction {
    __weak UITableView *weakTableView = mTableView;
    __weak FCXRefreshHeaderView *weakHeaderView = headerView;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        rows = 10;
        [weakTableView reloadData];
        [weakHeaderView endRefresh];
    });
}

- (void)loadMoreAction {
    __weak UITableView *weakTableView = mTableView;
    __weak FCXRefreshFooterView *weakFooterView = footerView;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        rows += 10;
        //        NSLog(@"---------%ld",(long)rows);
        [weakTableView reloadData];
        [weakFooterView endRefresh];
    });
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if(_titleArray.count >rows){
        
        return rows;
        
    }
    
    return _titleArray.count ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"cellIdentifier";
//    PaperInfoTableViewCell*cell=[tableView cellForRowAtIndexPath:indexPath];
    PaperInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    MAin * main = [_titleArray objectAtIndex:indexPath.row];
    NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"PaperInfoTableViewCell" owner:self options:nil];
    cell = [ array lastObject ];
    _paperTitle.text = main.name;
    _paperTitle.font = [UIFont systemFontOfSize:18];
    _paperTitle.textColor =[UIColor blueColor];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _userdefults1 = [NSUserDefaults standardUserDefaults];
    MAin * main = [_titleArray objectAtIndex:indexPath.row];
    NSString * number = [NSString stringWithFormat:@"%@",main.ID];
    [_userdefults1 setValue:number forKey:@"row1"];
//    点击 cell详情界面
    TableViewController * vcc = [[TableViewController alloc]init];
    vcc.myTitle = main.name;
    vcc.hidesBottomBarWhenPushed = YES;//跳转到二级界面时隐藏tabbar
    [self.navigationController pushViewController:vcc animated:NO];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
